package com.dmjj.cad.controller;

import com.dmjj.cad.dto.CategoryDTO;
import com.dmjj.cad.service.CategoryService;
import com.dmjj.cad.vo.CategoryInsertVO;
import com.dmjj.cad.vo.CategoryQueryVO;
import com.dmjj.cad.vo.CategoryUpdateVO;
import com.dmjj.cad.vo.CategoryVO;
import com.dmjj.cadcommon.base.BaseController;
import com.dmjj.cadcommon.result.ResultEnum;
import com.dmjj.cadcommon.result.ResultObject;
import com.dmjj.cadcommon.result.ResultPageObject;
import com.dmjj.cadcommon.util.ListCopyToVOUtil;
import com.dmjj.cadcommon.util.PaginationVOUtil;
import com.dmjj.cadcommon.vo.ResultPageVO;
import com.dmjj.cadcommon.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @Description 分类表控制器
 * @Author zhangxue
 * @Date 2019-04-19 10:39:23
 */

@RestController
@Api(tags = "分类表控制器")
@RequestMapping("category")
public class CategoryController extends BaseController {

    private static final Log LOGGER = LogFactory.getLog(CategoryController.class);

    @Autowired
    private CategoryService service;
    @Autowired
    private ResultObject resultObject;

    @Autowired
    private ResultPageObject resultPageObject;

    /**
	 * 分类表列表
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
	 * @Param
	 * @Return
	 */
    @ApiOperation(value = "分类表列表", response = CategoryVO.class)
    @PostMapping("list4page")
    public ResultPageVO<Object> list4Page(
            CategoryQueryVO entity,
            @ApiParam(value = "当前页", required = true) @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页显示条数", required = true) @RequestParam(value = "numPerPage", defaultValue = "15") Integer pageSize
    ){
        try {
            CategoryDTO entityDTO = new CategoryDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<CategoryDTO> list = service.list4Page(entityDTO, pageNum, pageSize);
            List<CategoryDTO> list1 = service.listByCondition(entityDTO);
            Integer total = list1.size();
            Integer totalPage = total/pageSize;
            if(total%pageSize!=0){
                totalPage += 1;
            }
            Boolean hasNext = false;
            if(pageNum!=null&&pageNum<totalPage){
                hasNext = true;
            }

            return resultPageObject.success(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),pageNum,totalPage,pageSize,total,hasNext,list);
        }catch (Exception e) {
            e.printStackTrace();
            return resultPageObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 分类表列表（无分页）
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "分类表列表（无分页）", response = CategoryVO.class)
    @PostMapping("list")
    public ResultVO<Object> listByCondition(CategoryVO entity){
        try {
            CategoryDTO entityDTO = new CategoryDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            List<CategoryDTO> list = service.listByCondition(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), ListCopyToVOUtil.getListToVO(list, CategoryVO.class));
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 分类表详细信息
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "分类表详细信息", response = CategoryVO.class)
    @PostMapping("byid")
    public ResultVO<Object> queryOneByPk(CategoryVO entity){
        try {
            CategoryDTO entityDTO = new CategoryDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            CategoryDTO entityDTO1 = service.queryOneByEntity(entityDTO);
            return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), entityDTO1);
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 保存分类表信息
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "保存分类表信息", response = ResultVO.class)
    @PostMapping("insert")
    public ResultVO<Object> save(CategoryInsertVO entity) {
        try {
            if(entity.getName()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 name 字段");
            }
            CategoryDTO entityDTO = new CategoryDTO();
            BeanUtils.copyProperties(entity, entityDTO);
            if(service.save(entityDTO) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),entityDTO);
            }else {
                return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 修改分类表信息
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "修改分类表信息", response = ResultVO.class)
    @PostMapping("update")
    public ResultVO<Object> update(CategoryUpdateVO entity) {
        try {

            if(entity.getName()==null){
                return resultObject.error(ResultEnum.FAIL.getCode(), "缺少 name 字段");
            }
            if(entity.getId()==null){
                //新增
                CategoryDTO entityDTO = new CategoryDTO();
                BeanUtils.copyProperties(entity, entityDTO);
                if(service.save(entityDTO) > 0) {
                    return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(),entityDTO);
                }else {
                    return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
                }
            }else{
                //修改
                CategoryDTO entityDTO = new CategoryDTO();
                BeanUtils.copyProperties(entity, entityDTO);
                if(service.edit(entityDTO) > 0) {
                    return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
                }else {
                    return resultObject.error(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
                }
            }

        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

    /**
     * 删除分类表信息
     * @Author zhangxue
     * @Date 2019-04-19 10:39:23
     * @Param
     * @Return
     */
    @ApiOperation(value = "删除分类表信息", response = ResultVO.class)
    @PostMapping("delete")
    public ResultVO<Object> delete(@ApiParam(value = "id",required = true) @RequestParam(value = "id") Long id){
        try {
            if(service.delete(id) > 0) {
                return resultObject.success(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg());
            }else {
                return resultObject.success(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg());
            }
        }catch (Exception e) {
            e.printStackTrace();
            return resultObject.error(ResultEnum.EXCEPTION.getCode(), ResultEnum.EXCEPTION.getMsg());
        }
    }

}